package cn.getech.data.metadata.controller.blood;

import cn.getech.data.intelligence.common.utils.R;
import cn.getech.data.metadata.entity.meta.CollectionTableInfo;
import cn.getech.data.metadata.service.CollectionBloodService;
import cn.getech.data.metadata.service.CollectionTableInfoService;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @description: 数据预览处理类
 * @author: wangzhaowen：kiss
 * @create: 2021/4/19 14:42
 * @version: 2021/4/19
 **/
@Slf4j
@Api(value="数据表详情",tags={"元数据表详情"})
@RestController
@RequestMapping("md/table")
public class BloodDataController {
    @Autowired
    private CollectionBloodService collectionBloodService;

    @Autowired
    private CollectionTableInfoService collectionTableInfoService;
    @ApiOperation("表血缘关系分析")
    @PostMapping("/relation")  ///  /data-metadata/md//table/relation
    public R tableRelation(@RequestBody BloodNode current) {
        if (current.getId() == null) {
            return R.error("表id不能为空");
        }
        JSONObject res = collectionBloodService.tableRelation(current);
        return R.okWithData(res);
    }
    @ApiOperation("字段血缘关系分析")
    @PostMapping("/field/relation")  ///  /data-metadata/md/table/field/relation
    public R fieldRelation(@RequestBody  BloodNode current) {
        if (current.getId() == null || StringUtils.isEmpty(current.getFieldName())) {
            return R.error("表id和字段名不能为空");
        }
        JSONObject res = collectionBloodService.tableFieldRelation(current);
        return R.okWithData(res);
    }

    @ApiOperation("解除异常")
    @PostMapping("/relation/delete")  ///  /data-metadata/md/table/field/relation
    public R relationDelete(@RequestBody  BloodUnion current) {
        if (current.getFrom() == null || StringUtils.isEmpty(current.getTo())) {
            return R.error("to和from不能为空");
        }
        collectionBloodService.relationDelete(current);
        return R.ok();
    }

    @ApiOperation("标记异常")
    @PostMapping("/relation/save")  ///  /data-metadata/md/table/relation/save
    public R relationSave(@RequestBody  BloodUnion current) {
        if (current.getFrom() == null || StringUtils.isEmpty(current.getTo())) {
            return R.error("to和from不能为空");
        }
        collectionBloodService.relationSave(current);
        return R.ok();
    }

    @ApiOperation("同步血缘")
    @PostMapping("/relation/init")  ///  /data-metadata/md/table/relation/init
    public R relationSave(@RequestBody BloodNode current) {
        collectionBloodService.synBloodData(current.getTableId());
        return R.ok();
    }

    @ApiOperation("信息")
    @GetMapping("/info/{id}")  // /data-metadata/md/table/info/{id}
    public R info(@PathVariable("id") Integer id) {
        CollectionTableInfo collectionTableInfo = collectionBloodService.getNodeInfo(id);
        return R.okWithData(collectionTableInfo);
    }
}
